/*
@STI LICENSE
(C) 2006-2008 Universit�t Innsbruck.

This file is part of myOntology.

myOntologyis free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

myOntologyis distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with myOntology.  If not, see <http://www.gnu.org/licenses/>.

@STI LICENSE */
/**
 * 
 */
package org.myontology.controller;

import info.aduna.iteration.CloseableIteration;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import org.openrdf.repository.RepositoryException;

/**
 *  @author <a href="michael.hackl@sti2.at">Michael Hackl</a>
 * Project: Myontology - www.myontology.org
 * STI
 */
public class SqlClosableIteration<T> implements CloseableIteration<T,RepositoryException> {

	private Collection<T> list = new ArrayList<T>();
	private boolean closed = false;
	private Iterator<T> it;
	
	
	public void add(T val) {
		list.add(val);
	}
	
	/* (non-Javadoc)
	 * @see info.aduna.iteration.CloseableIteration#close()
	 */
	public void close() throws RepositoryException {
		closed = true;
		list = null;

	}

	/* (non-Javadoc)
	 * @see info.aduna.iteration.Iteration#hasNext()
	 */
	public boolean hasNext() throws RepositoryException {
		if (it==null) {
			it = list.iterator();
		}
		boolean next = it.hasNext();
		if (!next) {
			it=null;
		}
		return next;
	}

	/* (non-Javadoc)
	 * @see info.aduna.iteration.Iteration#next()
	 */
	public T next() throws RepositoryException {
		if (it==null) {
			it = list.iterator();
		}
		return it.next();
	}

	/* (non-Javadoc)
	 * @see info.aduna.iteration.Iteration#remove()
	 */
	public void remove() throws RepositoryException {
		it.remove();

	}

}
